package com.yc.videoview;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;


/**
 * 7.1及以上需申请权限
 */
public class FloatPhone extends FloatView {
    private final Context mContext;
    private final WindowManager mWindowManager;

    private final WindowManager.LayoutConfig mLayoutParams;
    private ComponentContainer componentContainer;
    private int mX, mY;

    FloatPhone(Context applicationContext) {
        mContext = applicationContext;
        mWindowManager = WindowManager.getInstance();
        mLayoutParams = new WindowManager.LayoutConfig();
    }

    @Override
    public void setSize(int width, int height) {
        mLayoutParams.width = width;
        mLayoutParams.height = height;
    }

    @Override
    public void setView(Component view) {
        int layoutType;
        layoutType = WindowManager.LayoutConfig.
            MOD_APPLICATION;
        mLayoutParams.flags = WindowManager.LayoutConfig.MARK_TOUCHABLE_IMPOSSIBLE
            | WindowManager.LayoutConfig.MARK_FOCUSABLE_IMPOSSIBLE;
        mLayoutParams.type = layoutType;
        mLayoutParams.animations = 0;
        componentContainer.addComponent(view);
    }

    @Override
    public void setGravity(int gravity, int xOffset, int yOffset) {
        mLayoutParams.alignment = gravity;
        mLayoutParams.x = mX = xOffset;
        mLayoutParams.y = mY = yOffset;
    }


    @Override
    public void init() {
        PermissionActivity.request(mContext, new PermissionActivity.PermissionListener() {
            @Override
            public void onSuccess() {
                mWindowManager.addComponent(componentContainer, mContext, 0);
            }

            @Override
            public void onFail() {
            }
        });
    }

    @Override
    public void dismiss() {
    }

    @Override
    public void updateXY(int x, int y) {
        mLayoutParams.x = mX = x;
        mLayoutParams.y = mY = y;
    }

    @Override
    void updateX(int x) {
        mLayoutParams.x = mX = x;
    }

    @Override
    void updateY(int y) {
        mLayoutParams.y = mY = y;
        /* mWindowManager.updateViewLayout(mView, mLayoutParams);*/
    }

    @Override
    int getX() {
        return mX;
    }

    @Override
    int getY() {
        return mY;
    }
}
